﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using OrderPortal.Models;
using Runda.CMS.Order.Entity;
using Runda.CMS.Order.Repository;

namespace OrderPortal.Controllers
{

    [HandleError]
    public class AccountController : Controller
    {

        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }    

        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                Account account = null;
                if (MembershipService.ValidateUser(model.UserName, model.Password, out account))
                {
                    FormsService.SignIn(account);
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("List", "Order");
                    }
                }
                else
                {
                    ModelState.AddModelError("error", "输入用户名或密码错误，请重新输入");
                }
            } 
            return View(model);
        } 

        public ActionResult LogOff()
        {
            FormsService.SignOut();
            Dictionary<string, string> map = new Dictionary<string, string>();
            map.Add("result", "success");
            return Json(map, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UserInfo() {
            Account account = System.Web.HttpContext.Current.Session["currentUser"] as Account;
            Dictionary<string, string> map = new Dictionary<string, string>();
            map.Add("userName", account.accountName);
            return Json(map, JsonRequestBehavior.AllowGet);
        } 

        [HttpPost]
        public ActionResult ChangePassword(String password,String oldPassword)
        {
            if (ModelState.IsValid)
            {
                Account account = System.Web.HttpContext.Current.Session["currentUser"] as Account;
                Dictionary<string, string> map = new Dictionary<string, string>();
                string msg = "";
                if (MembershipService.ChangePassword(account.accountName,oldPassword,password,out msg))
                {              
                    map.Add("result","success");
                    return Json(map, JsonRequestBehavior.DenyGet);
                }
                else
                {
                    map.Add("result", "failture");
                    map.Add("msg", msg);
                    return Json(map, JsonRequestBehavior.DenyGet);
                }
            }             
            return View("");
        }        
    }
}
